# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import models, fields, api

class Task(models.Model):
    _name = 'my.task'
    _description = 'Task Management'

    name = fields.Char(string='Task Name', required=True)
    description = fields.Text(string='Description')
    deadline = fields.Date(string='Deadline')
    priority = fields.Selection([
        ('0', 'Low'),
        ('1', 'Medium'),
        ('2', 'High'),
    ], string='Priority', default='1')
    state = fields.Selection([
        ('draft', 'Draft'),
        ('in_progress', 'In Progress'),
        ('done', 'Done'),
        ('cancelled', 'Cancelled'),
    ], string='Status', default='draft')
    user_id = fields.Many2one('res.users', string='Assigned to')
    project_id = fields.Many2one('project.project', string='Project')

    @api.constrains('deadline')
    def _check_deadline(self):
        for task in self:
            if task.deadline and task.deadline < fields.Datetime.now():
                task.state = 'in_progress'

    def action_done(self):
        self.write({'state': 'done'})

    def action_cancel(self):
        self.write({'state': 'cancelled'})